﻿using CampWeek5__Book_Project_.Data;
using CampWeek5__Book_Project_.Data.Model;
using CampWeek5__Book_Project_.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CampWeek5__Book_Project_.Controllers
{
    public class DetailsController : Controller
    {
        // GET: Details
        //id is the ISBN
        public ActionResult BookDetail(int id)
        {
            BookVM model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Books.Where(x => x.ISBN == id).Select(x => new BookVM()
                {
                    AuthorId = x.AuthorId,
                    AuthorName = x.Author.AuthorName,
                    BookImg = x.BookImg,
                    DateDeleted = x.DateDeleted,
                    Id = x.Id,
                    ISBN = x.ISBN,
                    PubDate = x.PubDate,
                    Summary = x.Summary,
                    //go into the Book_Tags List on this book
                    //Select the Tag property on that Book_Tag
                    //Select the Description on the Tag
                    //Put all of the descriptions into a list
                    Tags = x.Book_Tags.Select(t => t.Tag.Description).ToList(),
                    TimesViewed = x.TimesViewed,
                    Title = x.Title,
                    Ratings = x.BookRatings.Where(r => r.DateDeleted == null).Select(r => new RatingVM()
                    {
                        BookRating = r.BookRating,
                        Comment = r.Comment,
                        DateDeleted = null,
                        Id = r.Id,
                        UserId = r.UserId,
                        Username = r.User.UserName

                    }).ToList(),
                   
                }).FirstOrDefault();
                

            }
            return View(model);
        }

        public ActionResult AuthorDetail(int id)
        {
            AuthorVM model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                //model = db.Authors.Include("Books").First(x => x.Id == id);
                model = db.Authors.Where(x => x.Id == id).Select(x => new AuthorVM()
                {
                    Id = x.Id,
                    AuthorName = x.AuthorName,
                    DateOfBirth = x.DateOfBirth,
                    DateOfDeath = x.DateOfDeath,
                    AuthorCountry = x.AuthorCountry,
                    AuthorImg = x.AuthorImg,
                    Tags = x.Author_Tags.Select(t => t.Tag.Description).ToList(),
                    AuthorBio = x.AuthorBio,
                    Books = x.Books.Where(b => b.AuthorId == x.Id).Select(b => new BookVM() 
                    { 
                        BookImg = b.BookImg,
                        ISBN = b.ISBN,
                        Title = b.Title
        
                    }).ToList()

                }).FirstOrDefault();
            }
            return View(model);
        }
    }
}